<?php
namespace App\Http\Controllers\Admin;
use App\Models\Admin\TvmMobileDetail;
use App\Models\Admin\TvmMobileCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Admin\AdminAction;
use App\Models\Admin\Currency;



class CurrencyController extends CommonController {

	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
	}


	/**
	 * 货币管理
	 **/
	public function currency(Request $request) {
		//货币列表
			$data = DB::table('tblc_currency')->orderBy('currencyid','DESC')->paginate(10);
		//展示模板
		return view('admin.currency.currencylist',[
			'data' => $data
		
		]);
	}


	/**
	 * add添加货币
	 **/
	public function add_currency(Request $request) {
		     	$id = $request->input('currencyid');
		    	if($request->isMethod('post')){
		        $id = $request->input('currencyid');
				$currency_type = $request->input('currency_type');
				if(!$currency_type){
					$this->ajax_error('货币英文简写不为空');
				}

				$data = array(
					'currencyid' => Currency::getNextSeq(),
					'currency_type' => $currency_type, 
					'exchange_rate' => $request->input('exchange_rate'), 
					'currency_unit' => $request->input('currency_unit'),
				);
				if(!$id){

					$result = Db::table('tblc_currency')->insert($data);
					if(!$result){
						$this->ajax_error('添加失败');
					}
						$this->ajax_success('添加成功');
				}else{
					$data['currencyid'] = $id;
					$result1 = Db::table('tblc_currency')->where('currencyid',$id)->update($data);
					if($result1){
						$this->ajax_success('编辑成功');
					}
				}
			
		
		}

				$condition = array();
				if($id>0){
					$condition[] = ['currencyid','=',$id];
				}else{
					$condition[] = ['currencyid','=',''];
				}
				$list =  Db::table('tblc_currency')->where($condition)->first();
				//展示模板
				return view('admin.currency.add_currency',[
				 'action'=>'add_currency',
				 'data'=>$list
				]);
	}

	//删除转账规则
	public function del(Request $request){
		$id = $request->input('id');
		$result = DB::table('tblc_currency')->where('currencyid',$id)->delete();
		if($result){
			$this->ajax_success('删除成功',$result);
		}
		$this->ajax_error('删除失败',$result);
	}


}